import { getInfo } from './utils/user'
import { get } from './utils/request'

App({
  onLaunch (options) {
    wx.getSystemInfo({
      success: event => {
        this.global.capsule =  wx.getMenuButtonBoundingClientRect()
        this.global.headerHeight = this.global.capsule.bottom + this.global.capsule.top - event.statusBarHeight
        this.global.statusBarHeight = event.statusBarHeight
      }
    })
    get('index/entry').then(res => {
      const { data } = res
      const { order, location } = data
      this.global.setting.order = order
      this.global.setting.location = location
      // wx.setStorageSync('setting_order', order)
      // wx.setStorageSync('setting_location', location)
    })
  },
  onError (error) {
    console.log('App Error:', error)
  },
  global: {
    setting: {},
    theme: '',
    title: '',
    color: '',
    tabbar: null
  },
  login: () => !!getInfo(),
  toast: (title, cb, icon = 'none') => {
    const duration = 1800
    wx.showToast({
      title: title,
      icon: icon,
      complete: () => {
        typeof cb === 'function' &&
          setTimeout(cb, duration)
      }
    })
  },
  redirect: (url) => {
    if (url.indexOf('http') >= 0) {
      wx.navigateTo({ url: '/pages/web/index?url=' + encodeURIComponent(url) })
    } else {
      wx.navigateTo({ url: url })
    }
  }
})